package proxy;

import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Set;

/**
 * @notes Getting the IP address of the current machine using Java
 * @author bo
 * @version 2019年6月26日 上午10:34:02
 */
public class NetworkUtils {

  /** 获取当前机器端口号 */
  public static String getLocalPort() throws Exception {
    MBeanServer mBeanServer = java.lang.management.ManagementFactory.getPlatformMBeanServer();
    Set<ObjectName> objectNames =
        mBeanServer.queryNames(new ObjectName("*:type=Connector,*"), null);
    if (objectNames == null || objectNames.size() <= 0) {
      throw new IllegalStateException(
          "Cannot get the names of MBeans controlled by the MBean server.");
    }
    for (ObjectName objectName : objectNames) {
      String protocol = String.valueOf(mBeanServer.getAttribute(objectName, "protocol"));
      String port = String.valueOf(mBeanServer.getAttribute(objectName, "port"));
      // windows下属性名称为HTTP/1.1, linux下为org.apache.coyote.http11.Http11NioProtocol
      if (protocol.equals("HTTP/1.1")
          || protocol.equals("org.apache.coyote.http11.Http11NioProtocol")) {
        return port;
      }
    }
    throw new IllegalStateException("Failed to get the HTTP port of the current server");
  }

  /** 获取当前机器的IP */
  public static String getLocalIP() throws Exception {
    InetAddress addr = InetAddress.getLocalHost();
    byte[] ipAddr = addr.getAddress();
    String ipAddrStr = "";
    for (int i = 0; i < ipAddr.length; i++) {
      if (i > 0) {
        ipAddrStr += ".";
      }
      ipAddrStr += ipAddr[i] & 0xFF;
    }
    return ipAddrStr;
  }

  public static void main(String[] args) throws Exception {
    //    System.out.println(NetworkUtils.getLocalIP());
    //    System.out.println(NetworkUtils.getLocalPort());

    try {
      InetAddress address =
          InetAddress.getLocalHost(); // 获取的是本地的IP地址 //PC-20140317PXKX/192.168.0.121
      String hostAddress = address.getHostAddress(); // 192.168.0.121
      System.out.println(address);
      System.out.println(address.getHostAddress());
      // www.baidu.com/14.215.177.37

      InetAddress address1 =
          InetAddress.getByName(
              "www.wodexiangce.cn"); // 获取的是该网站的ip地址，比如我们所有的请求都通过nginx的，所以这里获取到的其实是nginx服务器的IP地
      String hostAddress1 = address1.getHostAddress(); // 124.237.121.122

      InetAddress address2 = InetAddress.getByAddress(new byte[] {10, (byte) 200, 50, (byte) 175});
      //      InetAddress[] addresses =
      //          InetAddress.getAllByName("www.baidu.com"); // 根据主机名返回其可能的所有InetAddress对象
      //      for (InetAddress addr : addresses) {
      //        System.out.println(addr); // www.baidu.com/14.215.177.38
      //        System.out.println(addr.getHostAddress()); // www.baidu.com/14.215.177.38
      //        // www.baidu.com/14.215.177.37
      //      }
      System.out.println(address2); // www.baidu.com/14.215.177.38
      System.out.println(address2.getHostAddress()); // www.baidu.com/14.215.177.38
    } catch (UnknownHostException e) {
      e.printStackTrace();
    }
  }
}
